package com.hwd.gmall.order.listener;

import com.hwd.gmall.common.aspect.ListenMessage;
import com.hwd.gmall.common.constant.OrderConst;
import com.hwd.gmall.model.enums.OrderStatus;
import com.hwd.gmall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 订单超时支付的信息监听类
 *
 * @author 黄伟东/Victor
 * @date 2022/5/9 21:01
 */
@Component
@Log4j2
public class OrderPayTimeOutListener {

    @Resource
    private OrderService orderService;

    @RabbitListener(queues = OrderConst.ORDER_NORMAL_QUEUE)
    @ListenMessage(messageDescription = "订单超时的消息")
    public void orderTimeOutListener(Message message, Channel ignoredChannel) {

        // 获取消息的内容，orderId
        String[] split = new String(message.getBody()).split(":");
        long orderId = Long.parseLong(split[0]);
        String username = split[1];

        // 执行订单超时处理
        orderService.cancelOrder(orderId, OrderStatus.TIMEOUT.getComment(), username);

    }
}
